import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

public class RedisDemoString {

    public static void test1(Jedis jedis) {
        System.out.println("mset  和  mget");
        jedis.flushDB();

        jedis.mset("key1","111","key2","222","key3","333");

        List<String> values =  jedis.mget("key1", "key2", "key100" ,"key3");
        System.out.println("values = " + values);
    }

    public static void test2(Jedis jedis) {
        System.out.println("getrange 和  setrange");
        jedis.flushDB();

        jedis.set("key","abcdefjhigk");

        String result = jedis.getrange("key",2,5);
        System.out.println("result = " + result);

        long setrange = jedis.setrange("key", 2, "aaaa");
        System.out.println("setrange = " + setrange);

        System.out.println(jedis.get("key"));
    }

    public static void test3(Jedis jedis) {
        System.out.println("append");
        jedis.flushDB();

        jedis.set("key","abcdef");
        jedis.append("key","aaaa");

        System.out.println(jedis.get("key"));
    }

    public static void test4(Jedis jedis) {
        System.out.println("incr 和 decr");
        jedis.flushDB();

        jedis.set("key","100");

        long result = jedis.incr("key");
        System.out.println("result = " + result);

        String value = jedis.get("key");
        System.out.println("value = " + value);

        long result2 = jedis.decr("key");
        System.out.println("result2 = " + result2);

        System.out.println(jedis.incrBy("key", 100));
    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()){
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
            test4(jedis);
        }
    }
}
